package database.team.phase2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class SortedJoinIndex {
		public SortedJoinIndex(String [] listMinEmp,String [] listMaxEmp,String [] listMinDept,String [] listMaxDept ) throws IOException{
			String absPathEmp,absPathDept,valuePK,tupleContent,valueFK,tupleMatch = null;
			int empIDFKDept,empIDPK,minIndexEmp = 0, maxIndexEmp= 0,minIndexDept = 0, maxIndexDept= 0;
			File fileEmp = new File("TableEmp");
			File fileDept = new File("TableDept");
			//Read all the tmp files from TableEmp and TableDept".
			absPathEmp = fileEmp.getAbsolutePath();
			File folderEmp = new File(absPathEmp);
			File[] tempFilesEmp = folderEmp.listFiles();
			absPathDept = fileDept.getAbsolutePath();
			File folderDept = new File(absPathDept);
			File[] tempFilesDept = folderDept.listFiles();
			// Counting the number of files (Sublist) in order to start merging.
			int numOfSublistEmp= tempFilesEmp.length;
			int numOfSublistDept= tempFilesDept.length;
			BufferedReader[] tempFileEmp = new BufferedReader[numOfSublistEmp];
			BufferedReader[] tempFileDept = new BufferedReader[numOfSublistDept];
			FileWriter output = new FileWriter("SortedJoinBlocks.txt");
			PrintWriter tuple = new PrintWriter(output); 
			//Perform a join taking table Department as a Relation R and Employee as a relation S
			if(numOfSublistDept <= numOfSublistEmp){
			
				for(int i=0; i < numOfSublistDept; i++){
					tempFileDept[i] = new BufferedReader(new FileReader(tempFilesDept[i]));
					valueFK = tempFileDept[i].readLine();
					while (valueFK != null){
						empIDFKDept = Integer.parseInt(valueFK.substring(0, 7));
						//Looking for the index to search the key empIDFKDept
						for(int element = 0; element< listMinEmp.length; element++){
							minIndexEmp = Integer.parseInt(listMinEmp[element]);
							maxIndexEmp = Integer.parseInt(listMaxEmp[element]);
							if(empIDFKDept >= minIndexEmp && empIDFKDept <= maxIndexEmp ){
								tempFileEmp[element] = new BufferedReader(new FileReader(tempFilesEmp[element]));
								valuePK = tempFileEmp[element].readLine();
								while (valuePK != null){
									empIDPK = Integer.parseInt(valuePK.substring(0, 7));
									if(empIDPK == empIDFKDept){
										   tupleMatch = valuePK+valueFK;	
										   tupleContent = tupleMatch;
										   tuple.println(tupleContent);
									}
									valuePK = tempFileEmp[element].readLine();
								}
							}
								
						valueFK = tempFileDept[i].readLine();
						}
					
					 //Closing tuple and output readers and writers
					  tuple.close(); output.close();		
				}
			}
			}
			//Perform a join taking table Employee as a Relation R and Department as a relation S
			else
			{
				for(int i=0; i < numOfSublistEmp; i++){
					tempFileEmp[i] = new BufferedReader(new FileReader(tempFilesEmp[i]));
					valuePK = tempFileEmp[i].readLine();
					while (valuePK != null){
						empIDPK = Integer.parseInt(valuePK.substring(0, 7));
						//Looking for the index to search the key empIDFKDept
						for(int element = 0; element< listMinDept.length; element++){
							minIndexDept = Integer.parseInt(listMinDept[element]);
							maxIndexDept = Integer.parseInt(listMaxDept[element]);
							if(empIDPK >= minIndexDept && empIDPK <= maxIndexDept ){
							tempFileDept[element] = new BufferedReader(new FileReader(tempFilesDept[element]));
							valueFK = tempFileDept[element].readLine();
							while (valueFK != null){
								empIDFKDept = Integer.parseInt(valueFK.substring(0, 7));
								if(empIDPK == empIDFKDept){
									   tupleMatch = valuePK+valueFK;	
									   tupleContent = tupleMatch;
									   tuple.println(tupleContent);
								}
								valueFK = tempFileDept[element].readLine();
							}
						}
					valuePK = tempFileEmp[i].readLine();
					
				}
				//Closing tuple and output readers and writers
				  tuple.close(); output.close();
			  }	
			}
		  }
		}	
		
		public static void main(String[] args) throws IOException {
			 System.out.println("Joining the table Employee with Department");
			 long startTime = System.currentTimeMillis();
			 //new SortedJoinIndex();
			 long endTime = System.currentTimeMillis();
			 float totalTime = (endTime - startTime);
			 System.out.println("Total time to Join the tables: "+totalTime); 
			 System.out.println("The result is shown in the file SortedJoinBlocks.txt");
		 }
		
	}
